import React from 'react'
import { Table,Popconfirm,Button} from 'antd';
import * as conf from './config';
import {Link} from 'react-router';

class App extends React.Component {

  /**
   * 设置初始值
   * @param props
   */
  constructor(props){
    super(props);
    this.state = {
      loading: false,
      pagination: {
        defaultPageSize:conf.pageNumber,  //默认每页显示条数
        currentPage:1,  //当前页
      },
      data:[],
      params:{
        table:'m1c',
        where:{state:0}
      }
    };
  }

  /**
   * 第一次渲染后执行
   */
  componentDidMount() {
    this.getList();
  }

  /**
   * 获取数据
   * @param params
   */
  getList=()=>{
    const params = this.state.params;
    params.currentPage = this.state.pagination.currentPage;
    params.pageNumber = this.state.pagination.defaultPageSize;
    this.setState({ loading: true });
    conf.request(conf.action.list,params).then((result) => {
      if(result.errno == '0'){
        let data = result.data;
        const pagination = this.state.pagination;
        pagination.total = data.count; //获取总记录，设置分页
        this.setState({
          loading:false,
          data:data.data
        });
      }else{
        conf.tips('error','数据获取失败!',result.errmsg);
      }
    });
  };

  /**
   * 单个删除
   * @param id
   */
  del=(id)=>{
    this.setState({ loading: true });
    conf.request(conf.action.del,{
      table:this.state.params.table,
      where:{_id:id}
    }).then((result) => {
      conf.tips(result);
      this.setState({ loading: false });
      this.getList();
    });
  };

  /**
   * 批量删除
   */
  batchDel=()=>{
    this.setState({ loading: true });
    conf.request(conf.action.batchDel,{
      table:this.state.params.table,
      id:this.state.selectedRowKeys
    }).then((result) => {
      conf.tips(result);
      this.getList();
    });
  };

  /**
   * 点击分页，选择分类、事件
   * @param pagination
   * @param filters
   */
  handleTableChange = (pagination, filters) => {
    //获取当前点击页
    let page = this.state.pagination;
    page.currentPage = pagination.current;
    this.setState({
      pagination:page,
    });
    this.getList();
  };

  /**
   * 复选框改变事件
   * @param selectedRowKeys
   */
  onSelectChange = (selectedRowKeys) => {
    this.setState({
      selectedRowKeys:selectedRowKeys
    });
  };

  /**
   * 渲染模版
   * @returns {XML}
   */
  render() {
    const columns = [
      {title: '分类名称',
        dataIndex: 'name',
        width: '20%',
      },{
        title: '',
        width: '20%',
      }, {
        title: '',
        dataIndex: '',
        width: '25%',
      },{
        title: '添加时间',
        dataIndex: 'createTime',
        width: '10%',
      },{
        title: '状态',
        dataIndex: 'stateRemark',
        width: '10%',
      },{
        title: '操作',
        width: '10%',
        render: (text, record, index) => {
          const id = record._id;
          const url = '/admin/class/edit/'+id;
          return (
            <div>
              <Link to={url}>编辑</Link>
              <Popconfirm title="确定删除?" onConfirm={() => this.del(id)}>
              <Link style={{paddingLeft:10}}>删除</Link>
              </Popconfirm>
            </div>
          );
        }
      }
    ];

    const rowSelection = {
      onChange: this.onSelectChange,
    };
    return (
      <Table
        rowSelection={rowSelection}
        columns={columns}
        rowKey={record => record._id}
        dataSource={this.state.data}
        pagination={this.state.pagination}
        loading={this.state.loading}
        onChange={this.handleTableChange}
        footer={() =>  <Button onClick={this.batchDel}>删除</Button>}
      />
    );
  }
}

export default App;